
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
using System.Collections.Generic;
using System.Web.UI;

using Swaf.Container;
using Swaf.Gui;
using Swaf.Gui.AspNet2;

[assembly: WebResource("Syrinx.Gui.AspNet.Nitobi2.script.BaseLibrary.js", "application/x-javascript")]

namespace Syrinx.Gui.AspNet.Nitobi2
{
	public class NitobiBasePage : Swaf.Gui.AspNet2.BasePage
	{
		protected string m_ajaxHandler = "/AjaxRequestHandler.aspx";
		protected bool m_usingAjax = true;
		protected bool m_first = true;

		protected override void OnPreInit(EventArgs e)
		{
			string masterPage = MasterPageFile;
			MasterPageFile = masterPage;
			base.OnPreInit(e);
		}

		protected override void OnLoad(EventArgs e)
		{
			m_first = false;
			Response.Buffer = true;
			if (App.sessionData["UseAjax"] is bool)
				m_usingAjax = (bool)App.sessionData["UseAjax"];
			if (m_usingAjax)
			{
				string scriptUrl = ClientScript.GetWebResourceUrl(typeof(NitobiBasePage), "Syrinx.Gui.AspNet.Nitobi2.script.BaseLibrary.js");
				ClientScript.RegisterClientScriptInclude("SyrinxAjax", scriptUrl);
			}

			base.OnLoad(e);
			//if (Request.UserAgent.IndexOf("AppleWebKit") > 0)
			Request.Browser.Adapters.Clear();
			//Request.Browser.Adapters.Remove(typeof(Menu).AssemblyQualifiedName);
		}

		protected override void OnPreRender(EventArgs e)
		{
			//The following is Hack-ish type code to help preload known script libraries into the browser.  Without this,
			//if an nitobibizobjdisplay changes from a template that didnt have one of the below control to one that did,
			//The control would not initialize correctly because its base library wasnt available.  This ensures they are
			//available.
			if (m_usingAjax)
			{
				Nitobi.NitobiControlBase.SetupScripts(this);
				Combo.SetupScriptsAndStyleSheets(this, Nitobi.ComboThemeName.Tiger);
			}
			base.OnPreRender(e);

			if (m_usingAjax) 
			{
				StringBuilder buff = new StringBuilder(1024);
				buff.AppendFormat("var bizProcManager = new BizProcCoordinator('{0}', '{1}');\n", ViewName, Page.Response.ApplyAppPathModifier(m_ajaxHandler));
				foreach (ActiveBizProc bizproc in BPC.ActiveBizProcs.Values)
					buff.AppendFormat("var {0}BizProc = new ClientBizProc('{0}');\n", bizproc.BizProc.Id);
				ClientScript.RegisterClientScriptBlock(typeof(NitobiBasePage), "BizProcCoord", buff.ToString(), true);
			}
		}
		public bool UsingAjax { get { return m_usingAjax; } }
	}
}
